package com.ctg.behavior.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

public class CtDateUtils {
	
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss SSS";
	
	public static final String YYYY_MM_DD_8N = "yyyyMMdd";
	public static final String YYYY_MM_DD_HH_MM_SS_14N = "yyyyMMddHHmmss";
	
	public static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	private CtDateUtils() {
		
	}
	
	/*****************************************************************
	 * *字符串转日期
	 */
	//字符串格式采用默认格式：yyyy-MM-dd HH:mm:ss
	public static Date stringToDate(String dateStr) {
		return CtDateUtils.stringToDate(dateStr, YYYY_MM_DD_HH_MM_SS);
	}
	//通过参数据指定格式
	public static Date stringToDate(String dateStr, String dateFormat) {
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		Date date = null;
		try {
			date = formatter.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return date;
	}	
	//字符串格式采用默认格式：yyyy-MM-dd
	public static Date stringDayToDate(String dateStr) {
		return stringToDate(dateStr, YYYY_MM_DD);
	}
	
	/*****************************************************************
	 * *日期转字符串
	 */
	//日期格式采用默认格式：yyyy-MM-dd HH:mm:ss
	public static String dateToString(Date date) {
		return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(date);
	}
	//通过参数据指定格式
	public static String dateToString(Date date, String dateFormat) {
		//return DateFormatUtils.format(new Date(), dateFormat);
		return new SimpleDateFormat(dateFormat).format(date);
	}
	//获得当前时间字符串，默认格式为：yyyy-MM-dd HH:mm:ss
	public static String getCurrentDate() {
		return getCurrentDate(YYYY_MM_DD_HH_MM_SS);
	}
	//获得当前时间字符串，默认格式为：yyyy-MM-dd HH:mm:ss SS
	public static String getCurrentDateSSS() {
		return getCurrentDate(YYYY_MM_DD_HH_MM_SS_SSS);
	}
	//获得当前时间字符串, 需要指定时间格式
	public static String getCurrentDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	//得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	public static String formatDate(Date date, String pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length() > 0) {
			formatDate = DateFormatUtils.format(date, pattern);
		} else {
			formatDate = DateFormatUtils.format(date, YYYY_MM_DD_HH_MM_SS);
		}
		return formatDate;
	}
	//得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	public static String getDateTime(Date date) {
		return formatDate(date, YYYY_MM_DD_HH_MM_SS);
	}

	//得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	public static String getDateTime14N(Date date) {
		return formatDate(date, YYYY_MM_DD_HH_MM_SS_14N);
	}
	
	//得到当前日期和时间字符串 格式（yyyy-MM-dd）
	public static String getDate(Date date) {
		return formatDate(date, YYYY_MM_DD);
	}
	//得到当前时间字符串 格式（HH:mm:ss）
	public static String getTime(Date date) {
		return formatDate(date, "HH:mm:ss");
	}
	//得到当前年份字符串 格式（yyyy）
	public static String getYear(Date date) {
		return formatDate(date, "yyyy");
	}
	//得到当前月份字符串 格式（MM）
	public static String getMonth(Date date) {
		return formatDate(date, "MM");
	}
	//得到当天字符串 格式（dd）
	public static String getDay(Date date) {
		return formatDate(date, "dd");
	}
	//得到当天字符串 格式（HH）
	public static String getHour(Date date) {
		return formatDate(date, "HH");
	}
	//得到当天字符串 格式（mm）
	public static String getMinute(Date date) {
		return formatDate(date, "mm");
	}
	//得到当前星期字符串 格式（E）星期几
	public static String getWeekCN(Date date) {
		return formatDate(date, "E");
	}
	//星期日为每周第一天
	public static int getWeek(Date date) {
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);
	    int w = cal.get(Calendar.DAY_OF_WEEK);
	    return w;
	}
	
	/*****************************************************************
	 * *当前时间与参数中指定的时间运算
	 */
	// 获取过去的天数
	public static long pastDays(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (24 * 60 * 60 * 1000);
	}
	//获取过去的小时
	public static long pastHours(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 60 * 1000);
	}
	//获取过去的分钟
	public static long pastMinutes(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 1000);
	}
	//获取两个日期之间的天数(不足24小时按0计算)
	public static Integer diffDays(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return Double.valueOf((afterTime - beforeTime) / (1000 * 60 * 60 * 24)).intValue();
	}
	
	public static Date addMinute(Date date, int minuteCount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 计算分钟
		calendar.add(Calendar.MINUTE, minuteCount);
		return calendar.getTime();
	}
	
	public static Date addHour(Date date, int hourCount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 计算小时
		calendar.add(Calendar.HOUR_OF_DAY, hourCount);
		return calendar.getTime();
	}
	
	public static Date addDate(Date date, int dayCount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 计算天
		calendar.add(Calendar.DAY_OF_YEAR, dayCount);
		return calendar.getTime();
	}
	
	
	public static Date formatMinuteStartTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date formatMinuteEndTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	
	public static Date formatHourStartTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}
	
	public static Date formatHourEndTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		return calendar.getTime();
	}
	
	
	public static Date formatDayStartTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		return calendar.getTime();
	}
	
	public static Date formatDayEndTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		return calendar.getTime();
	}
	
	//获取当周第一天， 00:00:00
	public static Date formatWeekFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		
		//中国习惯：周一为周的第一天
		if(getWeek(date)==1) {
			calendar.add(Calendar.DAY_OF_YEAR, -1);
		}
		
		// 取周第一天(周日)
		calendar.set(Calendar.DAY_OF_WEEK, 1);
		
		// 中国习惯：周一为周的第一天
		calendar.add(Calendar.DAY_OF_YEAR, 1);

		return calendar.getTime();
	}
	//获取当周最后一天， 23:59:59
	public static Date formatWeekLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		
		//中国习惯：周一为周的第一天
		if(getWeek(date)==1) {
			calendar.add(Calendar.DAY_OF_YEAR, -1);
		}
		
		// 取周第一天
		calendar.set(Calendar.DAY_OF_WEEK, 1);
		
		// 取最后一天(周六)
		calendar.roll(Calendar.DAY_OF_WEEK, 6);
		
		// 中国习惯：周日为周的最后一天
		calendar.add(Calendar.DAY_OF_YEAR, 1);
		
		return calendar.getTime();
	}
	//获取当月第一天， 00:00:00
	public static Date formatMonthFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		// 取月第一天
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	//获取当月最后一天， 23:59:59
	public static Date formatMonthLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		// 取月第一天
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		// 取月最后一天
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
	}
	//获取当月第一天， 00:00:00
	public static Date formatYearFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);

		// 取月第一天
		calendar.set(Calendar.DAY_OF_YEAR, 1);

		return calendar.getTime();
	}
	//获取当年最后一天， 23:59:59
	public static Date formatYearLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 设定当前时间为0
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		// 取月第一天
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		// 取月最后一天
		calendar.add(Calendar.YEAR, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return calendar.getTime();
	}
	
	//毫秒转换为时间，默认格式：
	public static String formatMilliSeconds(long milliseconds) {
		Date date = new Date(milliseconds);
		String formatDateStr = CtDateUtils.dateToString(date);
		return formatDateStr;
	}
	
	//毫秒转换为时间
	public static String formatMilliSeconds(long milliseconds, String pattern) {
		Date date = new Date(milliseconds);
		String formatDateStr = CtDateUtils.dateToString(date, pattern);
		return formatDateStr;
	}

}
